package com.eastedu.superise.handler.basic;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastedu.manager.BasicResourceManager;
import com.eastedu.repository.basic.mapper.BasicResourceFileMapper;
import com.eastedu.repository.basic.po.BasicResourceFileDTO;
import com.eastedu.repository.basic.po.BasicResourcePO;
import com.eastedu.superise.handler.AbstractHistoricalDataHandler;
import com.eastedu.utils.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 〈〉
 *
 * @author ZHISHUAI.ZHANG
 * @since 2021 /7/28
 */
@Service
@DS("basic")
public class BasicFileHistoricalDataHandler extends AbstractHistoricalDataHandler<BasicResourcePO> {
    private static final List<String> OCRF_EXTENSIONS = Arrays.asList("ocrf", "crf", "crf2", " ocrf");
    private final BasicResourceFileMapper basicResourceFileMapper;
    private final BasicResourceManager basicResourceManager;

    /**
     * Instantiates a new Basic data source service.
     *
     * @param basicResourceFileMapper the basic resource file mapper
     * @param basicResourceManager    the basic resource manager
     */
    public BasicFileHistoricalDataHandler(BasicResourceFileMapper basicResourceFileMapper, BasicResourceManager basicResourceManager) {
        super();
        this.basicResourceFileMapper = basicResourceFileMapper;
        this.basicResourceManager = basicResourceManager;
    }


    @Override
    protected String getSql() {
        return "select r.* " +
                "from t_basic_resource r " +
                "         inner join tem_basic_resource_id_lzz f on r.id = f.id " +
                "order by r.id";
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void callback(BasicResourcePO po) {
        String resourceId = String.valueOf(po.getId());
        List<BasicResourceFileDTO> pos = basicResourceFileMapper.selectByResourceId(resourceId);

        // 如果已经有ocrf文件则不再处理
        if (pos.stream().anyMatch(file -> resourceId.equals(file.getBasicResourceId())
                && OCRF_EXTENSIONS.contains(file.getExtension().toLowerCase()))) {
            return;
        }

        List<BasicResourceFileDTO> ocrfFiles = pos.stream()
                .filter(file -> !resourceId.equals(file.getBasicResourceId()))
                .filter(file -> OCRF_EXTENSIONS.contains(file.getExtension().toLowerCase()))
                .sorted(Comparator.comparingLong(BasicResourceFileDTO::getCreateTime).reversed())
                .collect(Collectors.toList());
        // 查询到ocrf信息
        if (!CollectionUtils.isEmpty(ocrfFiles)) {
            if (insertResourceFile(resourceId, ocrfFiles)) {
                this.basicResourceManager.updateFileJsonByResourceId(resourceId);
            }
        }
    }

    private boolean insertResourceFile(String id, List<BasicResourceFileDTO> ocrfFiles) {
        if (CollectionUtils.isEmpty(ocrfFiles)) {
            return false;
        }
        basicResourceFileMapper.insert(ocrfFiles.get(0).copy(id));
        return true;
    }
}